package com.tenton.servlet;

import com.tenton.pojo.Position;
import com.tenton.service.Impl.PositionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "InsertPositionServlet",urlPatterns = "/insertPositionServlet")
public class InsertPositionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        PositionServiceImpl positionServiceImpl = new PositionServiceImpl();
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        Position position = new Position();
        position.setId(id);
        position.setPositionName(name);
        int i = positionServiceImpl.insertPosition(position);
        if (i == 1){
            request.getRequestDispatcher("/listPositionServlet").forward(request,response);
        }else {
            response.getWriter().write("职位新增失败，2秒后自动跳转到职位界面");
            response.setHeader("Refresh","3;URL=/listPositionServlet");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
